// pages/userSet/setusermsg/setusermsg.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenmodalput: true,
    name: "",
    columns: ["保密", "男", "女"],
    gender: 0 || wx.getStorageSync("gender") * 1,
    showModal: false,
  },
  nameSet() {
    this.setData({
      hiddenmodalput: false,
    })
  },
  returnMenu() {
    wx.navigateBack({
      delta: 1,
    })
  },
  cancelM: function (e) {
    this.setData({
      hiddenmodalput: true,
    })
  },
  iName: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  //修改昵称
  confirmM: function (e) {
    this.setData({
      hiddenmodalput: true,
    })
    var petname = this.data.name;
    let token = wx.getStorageSync('token')
    if (!/^[\w\u4e00-\u9fa5]+$/.test(petname)) {
      wx.showToast({
        title: '昵称格式错误',
        icon: 'none',
        duration: 1500
      })
    } else {
      wx.request({
        url: 'http://api_devs.wanxikeji.cn/api/userModify',
        data: {
          'token': token,
          'nick_name': petname
        },
        success: (res) => {
          if (res) {
            let openid = wx.getStorageSync('openid');
            wx.request({
              url: 'http://api_devs.wanxikeji.cn/api/refreshToken',
              method: 'POST',
              data: {
                openid: openid
              },
              success: (res) => {
                wx.setStorageSync('token', res.data.data.token);
                wx.setStorageSync('CXuserinfo', res.data.data);
                this.setData({
                  nicheng: res.data.data.nick_name
                })
              }
            });
          }
        }
      })
    }
  },

  //修改性别
  pickSex: function (e) {
    let sex = e.detail.value;
    if (sex == 0) {
      sex = 3
    }
    //  console.log(sex);
    let token = wx.getStorageSync('token');
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/userModify',
      data: {
        'token': token,
        'sex': sex
      },
      success: (res) => {
        if (res) {
          let openid = wx.getStorageSync('openid');
          wx.request({
            url: 'http://api_devs.wanxikeji.cn/api/refreshToken',
            method: 'POST',
            data: {
              openid: openid
            },
            success: (res) => {
              wx.setStorageSync('token', res.data.data.token);
              wx.setStorageSync('CXuserinfo', res.data.data);
              sex = res.data.data.sex;
              if (sex == 1) {
                sex = '男'
              } else if (sex == 2) {
                sex = '女'
              } else {
                sex = '保密'
              };
              this.setData({
                sex: sex
              })
            }
          });
        }
      }
    })
  },

  //头像修改
  headimage() {
    wx.chooseImage({
      count: 1, // 默认9     

      sizeType: ['original', 'compressed'],

      // 指定是原图还是压缩图，默认两个都有     

      sourceType: ['album', 'camera'],

      // 指定来源是相册还是相机，默认两个都有   

      success: (res) => {
        if (res) {
          let icon = res.tempFilePaths[0];
          console.log(icon);
          let token = wx.getStorageSync('token');
          wx.request({
            url: 'http://api_devs.wanxikeji.cn/api/userModify',
            data: {
              'token': token,
              'icon': icon
            },
            success: (res) => {
              if (res) {
                let openid = wx.getStorageSync('openid');
                wx.request({
                  url: 'http://api_devs.wanxikeji.cn/api/refreshToken',
                  method: 'POST',
                  data: {
                    openid: openid
                  },
                  success: (res) => {
                    wx.setStorageSync('token', res.data.data.token);
                    wx.setStorageSync('CXuserinfo', res.data.data);
                    this.setData({
                      userinfo: res.data.data
                    })
                  }
                });
              }
            }
          })
        }
      }

    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  onShow: function () {
    let userinfo = wx.getStorageSync('CXuserinfo');
    let nicheng = userinfo.nick_name;
    let sex = userinfo.sex;
    if (sex == 1) {
      sex = '男'
    } else if (sex == 2) {
      sex = '女'
    } else {
      sex = '保密'
    };
    this.setData({
      userinfo,
      nicheng,
      sex
    })

  },
})